#coding=utf-8
import Queue
import threading
import requests
from config import *

class trd(threading.Thread):
    def __init__(self,queue,lock):
        threading.Thread.__init__(self)
        self.queue=queue
        self.lock=lock
    def run(self):
        while not self.queue.empty():
            key=self.queue.get()

            if methed=="get":
                get(key)
            if methed=="post":
                post(key,self.lock)

def set_thread():
    #dict_count返回列表，使用字典个数及每个字典的密码个数
    threads=[]
    que=Queue.Queue()
    lock = threading.Lock()
    file=open(dictionary[0],"r")
    for i in range(dict_count()[0]):
        que.put(file.readline().replace("\n",""))
    for i in range(thread_count):
        threads.append(trd(que,lock))
    for i in range(thread_count):
        threads[i].start()
    for i in range(thread_count):
        threads[i].join()

def dict_count():
    di_count=[]
    for i in range(len(dictionary)):
        file=open(dictionary[i],"r")
        di_count.append(len(file.readlines()))
    return di_count

def get(key):
    pass

def post(key,lock):
    post_data[index[0]]=str(key)
    if len(dict_count())==1:

        print post_data
        respon=requests.post(url=url,data=post_data,headers=request_headers,timeout=3)
        lock.acquire()
        file = open(url.split("/")[-1]+".txt","a+")
        #file.write(str(respon.status_code)+"\r\n")
        #file.write(str(respon.headers)+"\r\n")
        file.write(str(respon.content)+"\r\n")
        file.write(key + "\r\n")
        file.close()
        lock.release()
    else:
        dict_list=[]
        file=open(dictionary[1],"r")
        for i in range(dict_count()[1]):
            dict_list.append(file.readline().replace("\n",""))
        for i in range(dict_count()[1]):
            post_data[index[1]]=dict_list[i]
            respon = requests.post(url=url, data=post_data, headers=request_headers)
            lock.acquire()
            file = open(url.split("/")[-1] + ".txt", "a+")
            file.write(str(respon.status_code) + "\r\n")
            file.write(str(respon.headers) + "\r\n")
            file.write(str(respon.content) + "\r\n")
            file.close()
            lock.release()
def main():
    set_thread()
if __name__=="__main__":
    main()